#include "icalendar.h"
#include <fstream>

namespace icslib {


ICalendar::~ICalendar()
{

}

bool ICalendar::LoadFileData() {
    string strLine, strNextLine;
    Component currentComponent = VCALENDAR;
    Component prevComponent = VCALENDAR;
    Event *pNewEvent = nullptr;
    Alarm newAlarm;
    // for getting some UIDs for events without them
    unsigned int nUID = 0;
    fstream file;
    file.open(m_strFileName.c_str(), ios::in | ios::binary);
    if(!file.is_open()) {

        return false;
    }

    getline(file, strNextLine);
    while (!file.eof()) {
        strLine = strNextLine;
        for (;;) {
           getline(file, strNextLine);
           if (strNextLine[0] != '\t' && strNextLine[0] != ' ')
               break;
           strLine += strNextLine.substr(1);
        }

        switch (currentComponent) {
        case VCALENDAR:
            if(strLine.find("BEGIN:VEVENT") == 0) {
                pNewEvent = new Event;
                currentComponent = VEVENT;
            }
            break;

        case VEVENT:
            if(strLine.find("UID") == 0) {
                pNewEvent->UID = GetProperty(strLine);
            }else if(strLine.find("SUMMARY") == 0){
                pNewEvent->Summary = GetProperty(strLine);
            } else if(strLine.find("DTSTAMP") == 0){
                pNewEvent->DtStamp = GetProperty(strLine);
            } else if(strLine.find("DTSTART") == 0){
                pNewEvent->DtStart = GetProperty(strLine);
            } else if(strLine.find("DTEND") == 0){
                pNewEvent->DtEnd = GetProperty(strLine);
            } else if(strLine.find("DESCRIPTION") == 0){
                pNewEvent->Description = GetProperty(strLine);
            } else if(strLine.find("RRULE") == 0){
                pNewEvent->RRule.Freq = ConvertFrequency(GetSubProperty(strLine, "FREQ"));
                pNewEvent->RRule.Interval = stoi(GetSubProperty(strLine, "INTERVAR"));
                if (pNewEvent->RRule.Interval == 0)
                    pNewEvent->RRule.Interval = 1;
                pNewEvent->RRule.Count = stoi(GetSubProperty(strLine, "COUNT"));
                 pNewEvent->RRule.Until = GetSubProperty(strLine, "UNTIL");
            }else if (strLine.find("BEGIN:VALARM") == 0) {
                newAlarm.Clear();
                prevComponent = currentComponent;
                currentComponent = VALARM;
            } else if (strLine.find("END:VEVENT") == 0) {
                if (pNewEvent->UID.empty())
                    pNewEvent->UID =  to_string(nUID++);

                m_pListEevent.push_back(pNewEvent);
                if(callback_ != nullptr) {
                    callback_(pNewEvent);
                }

                currentComponent = VCALENDAR;
            }
            break;

        }
    }
    return true;
}

void ICalendar::SetDataItemCb( std::function<void(icslib::Event *)>&f) {
   // if(m_fDataItemInfo == nullptr)
   //     m_fDataItemInfo = cbFun;
    callback_ = f;
}


}  // namespace icslib
